package com.ds.robot.assistant.service.impl;

import com.google.common.collect.Lists;
import com.ds.robot.assistant.config.DutyConfig;
import com.ds.robot.assistant.entity.mongo.QxUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作用：
 * db.qxUser.find(
 * { name: { $in: ["刘士波", "张兴兴", "李玉超", "李贝贝"] } },
 * { userId: 1, name: 1, position: 1, _id: 0 }
 * )
 *
 * @author WeiShaoying
 * 
 */
@Service
@Slf4j
public class QxUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DutyConfig dutyConfig;

    public Map<String, String> getUserMap() {
        List<QxUser> userList = findByName(dutyConfig.getPerson());
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        }
        return userList.stream().collect(Collectors.toMap(QxUser::getName, QxUser::getUserid, (oldVal, newVal) -> oldVal));
    }

    public List<QxUser> findByName(List<String> nameList) {
        log.info("mongoTemplate ={}", mongoTemplate);

        //构造参数，调用私有方法
        return findByCondition(nameList, Lists.newArrayList("JAVA", "前端", "产品", "测试"),
                Lists.newArrayList("工程师"),
                Lists.newArrayList("项目经理", "技术支持", "架构师"),
                Lists.newArrayList("UI", "算法", "数仓"));
    }

    private List<QxUser> findByCondition(List<String> nameList, List<String> startsWith,
                                         List<String> endsWith,
                                         List<String> equalsTo,
                                         List<String> contains) {
        Query query = new Query();
        Criteria nameCriteria = Criteria.where("name").in(nameList);

        Criteria positionCriteria = new Criteria();
        List<Criteria> orConditions = Lists.newArrayList();
        if (!startsWith.isEmpty()) {
            for (String prefix : startsWith) {
                orConditions.add(Criteria.where("position").regex("^" + prefix, "i"));
            }
        }
        if (!endsWith.isEmpty()) {
            for (String suffix : endsWith) {
                orConditions.add(Criteria.where("position").regex(suffix + "$", "i"));
            }
        }
        if (!equalsTo.isEmpty()) {
            for (String exactPos : equalsTo) {
                orConditions.add(Criteria.where("position").is(exactPos));
            }
        }
        if (!contains.isEmpty()) {
            for (String keyword : contains) {
                orConditions.add(Criteria.where("position").regex(keyword, "i"));
            }
        }
        positionCriteria.orOperator(orConditions);

        // 组合名字和职位条件
        Criteria combinedCriteria = new Criteria().andOperator(nameCriteria, positionCriteria);
        query.addCriteria(combinedCriteria);

        List<QxUser> qxUsers = mongoTemplate.find(query, QxUser.class);
        // List<QxUser> qxUsers = mongoTemplate.find(new Query(), QxUser.class);
        // List<QxUser> qxUsers = mongoTemplate.findAll(QxUser.class);
        log.info("====MongoDB查询结果:{}", qxUsers);
        return qxUsers;
    }

}
